package com.example.zyfypt516wh.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zyfypt516wh.activities.CollectUserActivity;
import com.squareup.picasso.Picasso;
import com.example.zyfypt516wh.R;
import com.example.zyfypt516wh.bean.FocusResult;
import com.example.zyfypt516wh.common.Common;
import com.example.zyfypt516wh.iface.FocusListener;
import com.example.zyfypt516wh.model.FocusModel;

import java.util.List;

public class FocusAdapter extends RecyclerView.Adapter {
    private List<FocusResult> list;//向 rv 中填充的数据
    private Context context;//上下文
    private LayoutInflater layoutInflater;//动态布局
    private String sessionID;
    String focusType="userfocus";


    //自定义 构造方法
    public FocusAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    public FocusAdapter(Context context, String sessionID) {
        this.sessionID=sessionID;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    //自定义 ViewHolder 子类， 容纳 item 视图

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView tvtitle, tvauthor, tvRole,tvFoucs;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            tvtitle = itemView.findViewById(R.id.textView);
            tvauthor = itemView.findViewById(R.id.textView2);
            tvRole = itemView.findViewById(R.id.textView3);
            tvFoucs=itemView.findViewById(R.id.un_fcous);
        }
    }

    // 自定义 设置数据 list
    public void setList(List<FocusResult> list) {
        this.list = list;
        notifyDataSetChanged();//通知 RV 刷新数据
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(
                R.layout.item_focus, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final FocusResult bean = list.get(position);
        if (bean == null)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        Picasso.get()//新版的 Picasso 方法改为 get()
                .load(Common.IMAGEURL )
                .placeholder(R.mipmap.ic_launcher)
                .into(viewHolder.imageView);

        Log.i("resAdapter---->", "bean----> "+bean.toString());
        viewHolder.tvtitle.setText(bean.getBean().getUsername());
        viewHolder.tvauthor.setText(bean.getBean().getRealname());
        viewHolder.tvRole.setText(bean.getBean().getRolename());
        //item 条目点击事件
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //取出当前 item 的 id
                String id = bean.getBean().getId();
                Common.USERID=Integer.parseInt(id);//将此用户id赋值给全局变量
                Intent intent=new Intent(context, CollectUserActivity.class);
                context.startActivity(intent);
//                if (type.equals("video")){
//                    Intent intent =new Intent(context, VideoActivity.class);
//                    intent.putExtra("id",id);
//                    intent.putExtra("type",type);
//                    intent.putExtra("userid",bean.getBean().getId());
//                    context.startActivity(intent);
//                }
//                else {
//                    Intent intent =new Intent(context, WebActivity.class);
//                    intent.putExtra("id",id);
//                    intent.putExtra("type",type);
//                    intent.putExtra("userid",bean.getBean().getId());
//                    context.startActivity(intent);
//                }


                Toast.makeText(context, "" + id, Toast.LENGTH_SHORT).show();

            }
        });

        //取消关注点击
        viewHolder.tvFoucs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String id=bean.getBean().getId();
                unFollow(id);
                list.remove(bean);
                notifyDataSetChanged();

            }
        });
    }

    FocusListener focusListener =new FocusListener() {
        @Override
        public void onResponse(List<FocusResult> list) {

        }

        @Override
        public void onResponse(String res) {
            Log.i("TAG", "取消关注结果:---》 "+res);
        }

        @Override
        public void onFail(String msg) {
            System.out.println("判断是否关注请求--->"+msg);
        }
    };
    /**
     * 取消关注
     */
    private void unFollow(String id) {
        FocusModel model=new FocusModel();
        model.unfocus(focusType,Integer.parseInt(id),sessionID, focusListener);
    }

    @Override
    public int getItemCount() {
        if (list != null)
            return list.size();
        else
            return 0;
    }
}
